package com.depin.pdaapp.utils;

import java.text.NumberFormat;

/**
 * Created by lzn.
 * Date：2020/5/19 14:32
 * Desc：
 **/
public class ProportionUtil {
    /**
     * 计算百分比 整数
     *
     * @param divisor
     * @param dividend
     * @return
     */
    public static String proportionInt(Integer divisor, Integer dividend) {
        if (dividend == null || divisor == null)
            return null;
        NumberFormat numberFormat = NumberFormat.getInstance();
        // 设置精确到小数点后2位
        numberFormat.setMaximumFractionDigits(2);
        String result = numberFormat.format((float) divisor / (float) dividend * 100);
        if (result.indexOf(".") != -1) {
            result = String.valueOf(Math.round(Double.parseDouble(result)));
        }
        return result+"%";
    }

    /**
     * 计算百分比 整数
     *
     * @param divisor
     * @param dividend
     * @return
     */
    public static String proportionInt(Double divisor, Double dividend) {
        if (dividend == null || divisor == null)
            return null;
        NumberFormat numberFormat = NumberFormat.getInstance();
        // 设置精确到小数点后2位
        numberFormat.setMaximumFractionDigits(2);
        String result = numberFormat.format(divisor / dividend * 100);
        if (result.indexOf(".") != -1) {
            result = String.valueOf(Math.round(Double.parseDouble(result)));
        }
        return result+"%";
    }

    public static String doubleTransform(double num) {
        String strNum = num + "";
        int a = strNum.indexOf(".");
        if (a > 0) {
            //获取小数点后面的数字
            String dianAfter = strNum.substring(a + 1);
            if ("0".equals(dianAfter)) {
                return strNum + "0";
            } else {
                if (dianAfter.length() == 1) {
                    return strNum + "0";
                } else {
                    return strNum;
                }
            }
        } else {
            return strNum + ".00";
        }
    }

}
